package com.lan.tian;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Scanner;

/**
 * @author lovvyang
 * @date 2020/5/24-9:31
 */
/*
 * @PackageName:com.lan.tian
 * @ClassName:Demo
 * @Description
 * @author lovvyang
 * @date 2020/5/24/9:31
 **/
/*
* statement:sql拼接麻烦/sql注入/无法处理blob等于二进制的数据
*
*
*
*
*
* */


public class Demo {
    public static void main(String[] args)throws Exception {
        System.out.println(".....");

        //注册驱动
        Class.forName("com.mysql.jdbc.Driver");
        //连接数据库
        String url="jdbc:mysql://localhost:3306/zuoye";
        Connection connection = DriverManager.getConnection(url, "root", "123456");
        //编写sql语句
        String sql="select * from zuoye.teacher";
        //执行sql语句
        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);
        //处理结果
        while (resultSet.next()){
            String Tno = resultSet.getString(1);
            String Tname = resultSet.getString(2);
            System.out.println(Tno+"--"+Tname);
        }
        //关闭资源
        statement.close();
        connection.close();
    }
    @Test
    public void test()throws  Exception{
        Scanner scanner=new Scanner(System.in);

        System.out.println("请输入id");
        String id = scanner.next();
        System.out.println("姓名");
        String xing = scanner.next();
        System.out.println("性别");
        String bie = scanner.next();
        System.out.println("生日");
        String ri = scanner.next();


        System.out.println("职务");
        String zhi = scanner.next();
        System.out.println("科系");
        String xi = scanner.next();
        Class.forName("com.mysql.jdbc.Driver");
        String url="jdbc:mysql://localhost:3306/zuoye";
        Connection connection = DriverManager.getConnection(url,"root","123456");
        String sql="insert into zuoye.teacher values('"+id+"','"+xing+"','"+bie+"','"+ri+"','"+zhi+"','"+xi+"')";
        Statement statement = connection.createStatement();
        int i = statement.executeUpdate(sql);
        System.out.println(i);
        connection.close();
        statement.close();


    }

}
